﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using HNG.LIB.Localization;
public partial class AdminPortal_Controls_Account_User_Info : System.Web.UI.UserControl
{
    TblThanhVienManager _TblThanhVienManager = new TblThanhVienManager();
    public int CusID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FunctionExtend.LoadGender(ddlGender);

            try { CusID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { CusID = 0; }

            if (CusID == 0) //Add cate
            {
                lblTitle.Text = "Thêm thành viên";
                Toolbox1.ShowDel = false;
                Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
                txtUserName.Enabled = true;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa thành viên: " + CusID;
                BindData(CusID);
            }
        }
    }


    //load chi tiet san pham
    protected void BindData(int ID)
    {
        TblThanhVienEntity _TblThanhVienEntity = _TblThanhVienManager.SelectOne(ID);
        if (_TblThanhVienEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        txtID.Text = ID.ToString();
        hidID.Value = ID.ToString();
        txtUserName.Text = Server.HtmlDecode(_TblThanhVienEntity.TenTruyCap);
        txtFullName.Text = _TblThanhVienEntity.FullName;
        txtEmail.Text = Server.HtmlDecode(_TblThanhVienEntity.Email);
        txtPhone.Text = _TblThanhVienEntity.DienThoai.ToString();
        txtAddress.Text = Server.HtmlDecode(_TblThanhVienEntity.DiaChi);
        ddlGender.SelectedValue = _TblThanhVienEntity.GioiTinh.ToString();
        chkStatus.Checked = Convert.ToBoolean(_TblThanhVienEntity.TrangThai);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblThanhVienManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtUserName.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tên người gửi !</span>"; txtUserName.Focus(); return; }
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/UserEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        TblThanhVienEntity _TblThanhVienEntity = new TblThanhVienEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblThanhVienEntity.Id = Convert.ToInt32(hidID.Value);
        else
        {
            _TblThanhVienEntity.NgayTao = DateTime.Now;
            _TblThanhVienEntity.MatKhau = LocalizationUtils.MaHoaMatKhau("123456");
        }
        _TblThanhVienEntity.TenTruyCap = Server.HtmlEncode(txtUserName.Text);
        _TblThanhVienEntity.FullName = txtFullName.Text;
        _TblThanhVienEntity.Email = Server.HtmlEncode(txtEmail.Text);
        _TblThanhVienEntity.DiaChi = Server.HtmlEncode(txtAddress.Text);
        _TblThanhVienEntity.GioiTinh = ddlGender.SelectedValue;
        _TblThanhVienEntity.DienThoai = txtPhone.Text;
        _TblThanhVienEntity.TrangThai = chkStatus.Checked;        
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblThanhVienManager.Update(_TblThanhVienEntity);
        }
        else
        {
            _TblThanhVienManager.Insert(_TblThanhVienEntity);
            hidID.Value = _TblThanhVienEntity.Id.ToString();
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
}